<#include "/able/_inc/_head.html"/>
<#include "/able/_inc/_layout.html"/>

<!DOCTYPE HTML>
<!--[if IE 8]><html class="ie8" lang="en"><![endif]-->
<!--[if IE 9]><html class="ie9" lang="en"><![endif]-->
<!--[if !IE]><!-->
<html lang="${lang}">

<head>
    <@head />
</head>
<body>
<@layout>
<div class="wrap-content" id="container">
<!-- start: PAGE TITLE -->
<section id="page-title" >
    <div class="row">
        <div class="col-sm-8">
            <a href="${ctx_admin}/category" class="btn btn-o btn-primary" style="border-radius: 50px;padding: 5px 8px"><i class="fa fa-reply"></i></a>
            <label>内容管理 - 栏目</label>
        </div>

    </div>
</section>
<!-- end: PAGE TITLE -->
<!-- start: BASIC TABLE -->

<div class="container-fluid container-fullw bg-white">

    <div class="row">
        <div class="col-md-12">
            <!--<h5 class="over-title margin-bottom-15">Basic <span class="text-bold">Table</span></h5>-->
            <!--<p>-->
            <!--For basic styling&mdash;light padding and only horizontal dividers&mdash;add the base class <code>.table</code> to any <code>&lt;table&gt;</code>. It may seem super redundant, but given the widespread use of tables for other plugins like calendars and date pickers, we've opted to isolate our custom table styles.-->
            <!--</p>-->
            <div class="alert alert-info">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
                <div><i class="fa fa-question-circle"></i>操作提示</div>
                <ul>
                    <li>标识“<em>*</em>”的选项为必填项，其余为选填项。</li>
                    <li>上传图片时请注意图片尺寸问题。</li>
                </ul>
            </div>

            <div class="col-lg-2 col-md-12"></div>
            <div class="col-lg-8 col-md-12">
                <form id="form" role="form" class="form-horizontal" action="${ctx_admin}<#if model?exists>/category/update<#else>/category/add</#if>">

                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="categoryName">
                            栏目名称：<span class="symbol required" aria-required="true"></span>
                        </label>
                        <div class="col-sm-7">
                            <input type="hidden" id="categoryId" name="categoryId" value="${model.categoryId}">
                            <input id="categoryName"  name="categoryName" type="text" class="form-control required" placeholder="" value="${model.categoryName}">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="categoryPId">
                            上级栏目：<span class="symbol required" aria-required="true"></span>
                        </label>
                        <div class="col-sm-7">
                            <input type="text" class="categoryPId form-control required" id="categoryPId" name="categoryPId" placeholder="" readonly="readonly"   value="${model.categoryPId}">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="categoryOrder">
                            排序：
                        </label>
                        <div class="col-sm-7">
                            <input type="text" id="categoryOrder" name="categoryOrder" placeholder="排序" class="form-control" value="${model.categoryOrder}">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="keywords">
                            关键字：
                        </label>
                        <div class="col-sm-7">
                            <input id="keywords" type="text" name="keywords" placeholder="关键字" class="form-control" value="${model.keywords}" >
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="template">
                            模板页面：
                        </label>
                        <div class="col-sm-7">
                            <input id="template" type="text" name="template" placeholder="模板页面" class="form-control" value="${model.template}" >
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="description">
                            描述说明：
                        </label>
                        <div class="col-sm-7">
                            <textarea id="description" name="description" placeholder="描述说明" class="form-control" >${model.description}</textarea>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="showModes0">
                            是否显示：
                        </label>
                        <div class="col-sm-7">
                            <div class="clip-radio radio-primary">
                                <input id="showModes0" type="radio" name="showModes" value="0" <#if model.showModes=='0'> checked="checked" </#if>>
                                <label for="showModes0">
                                    隐藏
                                </label>
                                <input id="showModes1" type="radio" name="showModes" value="1" <#if model.showModes=='1'> checked="checked" </#if>>
                                <label for="showModes1">
                                    显示
                                </label>
                            </div>
                        </div>

                    </div>
                    <div class="form-group margin-bottom-0">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button class="btn  btn-primary" type="submit">
                                确定
                            </button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="col-lg-2 col-md-12"></div>
        </div>
    </div>
</div>

    </@layout>
<!-- The main application script -->
<!--<script src="vendor/jquery-file-upload/main.js"></script>-->
<!-- <script type="text/javascript" src="assets/js/ad/ad-add.js"></script> -->
<script>

    jQuery(document).ready(function() {

        $.ajax({
			type: "POST",
			data: {},
			dataType: 'json',
			url: global.adminPath + '/category/tree',
			success: function(res){
				$("#categoryPId").droptree({
					items:res.data , 
                    transition:"ztree",
                    idLabel:"categoryId", 
                    textLabel:"categoryName", 
                    pidLabel:"categoryPId",
                    rootPId:0
                });
			}
		});
    });
</script>
<script type="text/javascript">

    var validateCheckRadio = function (val) {
        $("input[type='radio'], input[type='checkbox']").on('ifChecked', function(event) {
            $(this).parent().closest(".has-error").removeClass("has-error").addClass("has-success").find(".help-block").hide().end().find('.symbol').addClass('ok');
        });
    };
    var FormValidator = function () {
        "use strict";
        // function to initiate Validation Sample 2
        var runValidator2 = function () {
            var form2 = $('#form');
            var errorHandler2 = $('.errorHandler', form2);
            var successHandler2 = $('.successHandler', form2);

            $.validator.addMethod("categoryPId", function () {
                if ($("#categoryPId").val() != "") {
                    return true;
                } else {
                    return false;
                }
            }, 'This field is required.');
            form2.validate({
                errorElement: "span", // contain the error msg in a small tag
                errorClass: 'help-block',
                errorPlacement: function (error, element) { // render error placement for each input type
                	
                    if (element.attr("type") == "radio" || element.attr("type") == "checkbox") { // for chosen elements, need to insert the error after the chosen container
                        error.insertAfter($(element).closest('.form-group').children('div').children().last());
                    } else if(element.hasClass("categoryPId")) {
                    	error.appendTo($(element).closest('.form-group'));
                    } else {
                        error.insertAfter(element);
                        // for other inputs, just perform default behavior
                    }
                },
                ignore: "",
                rules: {
                	categoryPId:'categoryPId'
                },
                messages: {

                },
                invalidHandler: function (event, validator) { //display error alert on form submit
                    successHandler2.hide();
                    errorHandler2.show();
                },
                highlight: function (element) {
                    $(element).closest('.help-block').removeClass('valid');
                    // display OK icon
                    $(element).closest('.form-group').removeClass('has-success').addClass('has-error').find('.symbol').removeClass('ok').addClass('required');
                    // add the Bootstrap error class to the control group
                },
                unhighlight: function (element) { // revert the change done by hightlight
                    $(element).closest('.form-group').removeClass('has-error');
                    // set error class to the control group
                },
                success: function (label, element) {
                    label.addClass('help-block valid');
                    // mark the current input as valid and display OK icon
                    $(element).closest('.form-group').removeClass('has-error').addClass('has-success').find('.symbol').removeClass('required').addClass('ok');
                },
                submitHandler: function (form) {
                    successHandler2.show();
                    errorHandler2.hide();

                    $.ajax({
                        type:form.method,
                        url:form.action,
                        async:false, //同步方法，如果用异步的话，flag永远为1
                        dataType:'json',
                        data:$(form).serialize(),
                        success: function(res){
                            if(res.success){
                                layer.msg(res.message, {
                                    icon: 1,
                                    time: 2000, ///2秒关闭（如果不配置，默认是3秒）
                                }, function(){
                                    location.href= global.adminPath+"/category";
                                });

                            }else{
                                layer.msg(res.message, {icon: 2});
                            }
                        }
                    });
                }
            });
            //CKEDITOR.disableAutoInline = true;
            //$('textarea.ckeditor').ckeditor();
        };
        return {
            //main function to initiate template pages
            init: function () {
                validateCheckRadio();
                runValidator2();
            }
        };
    }();

    jQuery(document).ready(function() {
        FormValidator.init();
    });
</script>